/*
 * Created by SharpDevelop.
 * User: K 3 N ! L
 * Date: 10/25/2008
 * Time: 4:06 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Form1.
	/// </summary>
	public partial class Form1 : Form
	{
		DataSet dsViewOrder = null;
		
		private static string strCon = "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=./Data/DB.mdb" ;
		private OleDbConnection myCon = new OleDbConnection(strCon);
		
		public Form1()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
		}
		
		void OrdersLoad(object sender, EventArgs e)
		{
			//disableAllControls(null, null);
			RefreshDataGrid();
		}
		
		public DataSet View()
		{
			//string sqlStr = "SELECT orders.order_id, product_name, customers.customer_name, orders.lr_no, transporters.transporter_name FROM orders, order_products, products, customers, transporters WHERE orders.order_id=order_products.order_id And products.product_id=order_products.product_id And customers.customer_id=orders.customer_id And transporters.transporter_id=orders.transporter_id;";
			//string sqlStr = "SELECT ord.order_id, cust.Customer_Name, tx.Transporter_Name, ord.lr_no FROM Orders AS ord, Customers AS cust, Transporters AS tx WHERE ord.Customer_id=cust.Customer_id And ord.Transporter_ID=tx.Transporter_ID";
			string sqlStr = "SELECT ORD.ORDER_ID , CUST.CUSTOMER_NAME, ORD.TOTAL_AMOUNT, TX.TRANSPORTER_NAME, ORD.LR_NO, ORD.PAYMENT_RECEIVED FROM ORDERS AS ORD, CUSTOMERS AS CUST, TRANSPORTERS AS TX WHERE ORD.CUSTOMER_ID=CUST.CUSTOMER_ID AND ORD.TRANSPORTER_ID=TX.TRANSPORTER_ID";
			//Make a DataSet object
			DataSet myDataSet = new DataSet();
			//Using the OleDbDataAdapter execute the query
			OleDbDataAdapter myAdapter = new OleDbDataAdapter( sqlStr , myCon );
			//Fill the DataSet with the Table 'Product'
			myAdapter.Fill(myDataSet,"ViewOrder");
			return myDataSet;
		}
		
		public void RefreshDataGrid()
		{
			dsViewOrder = View();

			foreach(DataRow drPaymentCheck in dsViewOrder.Tables[0].Rows)
			{
				bool paymentCheck = (bool)drPaymentCheck[5];
				//MessageBox.Show("refresh "+paymentCheck);
				if(!paymentCheck)
				{
					datagridOrders.ParentRowsBackColor = System.Drawing.Color.Red;
					//datagridOrders.BackColor = System.Drawing.Color.Red;
					//MessageBox.Show("in if backcolor");
				}
			}
			
			datagridOrders.SetDataBinding(dsViewOrder,"ViewOrder");
			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			this.Close();
			
		}
		
		void DataGridOrdersCurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
			if(datagridOrders.CurrentCell.ColumnNumber == 0)
			{
				ViewOrderProducts viewOrdProd = new ViewOrderProducts();
				//MessageBox.Show("hello : "+Convert.ToString(datagridOrders.CurrentRowIndex));
				viewOrdProd.dr = dsViewOrder.Tables[0].DefaultView[datagridOrders.CurrentRowIndex].Row;
				
				string orderId = viewOrdProd.dr[0].ToString();
				string custName = viewOrdProd.dr[1].ToString();
				string totalAmt = viewOrdProd.dr[2].ToString();
				string xPorter = viewOrdProd.dr[3].ToString();
				string LRNo = viewOrdProd.dr[4].ToString();
				
				string[] orderProductsDetails = {orderId, custName, totalAmt, xPorter, LRNo};
				
				
				MessageBox.Show(orderId+" : "+custName+" : "+totalAmt+" : "+xPorter+" : "+LRNo);
					
				viewOrdProd.RefreshDataGrid(orderProductsDetails);
				viewOrdProd.ShowDialog();
			}
			
			else if(datagridOrders.CurrentCell.ColumnNumber == 5)
			{
				DataRow dr = dsViewOrder.Tables[0].DefaultView[datagridOrders.CurrentRowIndex].Row;
				bool payment_check = (bool)dr["PAYMENT_RECEIVED"];
				MessageBox.Show("Payment_Check : "+payment_check);
				//dsViewOrder.Tables[0].DefaultView[datagridOrders.CurrentRowIndex].Row.BeginEdit();
				if(payment_check)
				{
					//MessageBox.Show("in : "+payment_check);
//					dsViewOrder.Tables[0].DefaultView[datagridOrders.CurrentRowIndex].Row.ItemArray[5] = true;
//					dsViewOrder.Tables[0].AcceptChanges();
//					RefreshDataGrid();
				}
			}
			}
			catch(Exception e1)
			{
				MessageBox.Show("No Orders Placed : "+e1.ToString());
			}
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			bool statusUpdate = false;
			foreach(DataRow dr in dsViewOrder.Tables[0].Rows)
			{
				bool payment_check = (bool)dr["PAYMENT_RECEIVED"];
				int orderId = Convert.ToInt32(dr["ORDER_ID"]);

				//MessageBox.Show("Btn Click : "+payment_check);
				
				statusUpdate = Update(orderId, payment_check);

				if(!statusUpdate)
				{
					MessageBox.Show("Sorry Your Request Could Not Be Processed.");
					break;
				}
			}
		}
		
		public bool Update(int id, bool paymentCheck)
		{
			bool done = false;
			
			if( id > 0 )
			{
				string sqlStr = "Update Orders SET ";
				sqlStr += "Payment_Received = " + paymentCheck + " ";
				sqlStr += "WHERE  ORDER_ID= " + Convert.ToString(id);
				
				OleDbCommand myCmd = new OleDbCommand( sqlStr , myCon ) ;
				
				try
				{
					//MessageBox.Show(sqlStr);
					myCon.Open();
					
					//Execute the statement
					myCmd.ExecuteNonQuery();
					//statusBar.Text="Connected - Now you can add records";
					done = true;
					
				}
				catch( OleDbException a)
				{
					MessageBox.Show("Error in updating! \n" + a.ToString(), "Error");
				}
				finally
				{
					myCmd.Dispose();
					myCon.Close() ;
					//myCon.Dispose();
				}
			}
			else
			{
				MessageBox.Show("All mandatory fields must be filled.", "Error");
			}
			return done;
		}
		
		void BtnRefreshClick(object sender, EventArgs e)
		{
			RefreshDataGrid();
		}
	}
}
